﻿/*============================================================================
  File:    Simple Sample AMO process.cs

  Summary: Contains the code to do the process on the objects and to generate
           the traces of the process execution

           Part of Simple Sample AMO Access

  Date:    November 2006
------------------------------------------------------------------------------
  This file is part of the Microsoft SQL Server Code Samples.

  Copyright (C) Microsoft Corporation.  All rights reserved.

  This source code is intended only as a supplement to Microsoft
  Development Tools and/or on-line documentation.  See these other
  materials for detailed information regarding Microsoft code samples.

  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
============================================================================*/

#region Using directives

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.Sql;
using Microsoft.AnalysisServices;

#endregion

namespace Microsoft.Samples.SqlServer
{
    public partial class SimpleSampleAMOAccess
    {

        public void ProcessDatabase( String processTypeSelected)
        {
            Database db = svr.Databases[DatabaseName];

            // Start the default trace
            svr.SessionTrace.OnEvent
                += new TraceEventHandler(DefaultTrace_OnEvent);
            svr.SessionTrace.Stopped
                += new TraceStoppedEventHandler(DefaultTrace_Stopped);
            svr.SessionTrace.Start();


            // Process the database
            // The trace handlers will output all progress notifications 

            foreach (Dimension dim in db.Dimensions)
            {
                if (dim.State == AnalysisState.Unprocessed)
                {
                    dim.Process(ProcessType.ProcessFull);
                }
                else
                {
                    dim.Process(ProcessType.ProcessDefault);
                }
            }

            if (db.Cubes[CubeName].State == AnalysisState.Unprocessed)
            {
                db.Cubes[CubeName].Process(ProcessType.ProcessFull);
            }
            else
            {
                switch (processTypeSelected)
                {
                    case "ProcessFull":
                        db.Cubes[CubeName].Process(ProcessType.ProcessFull);
                        break;
                    case "ProcessIndexes":
                        db.Cubes[CubeName].Process(ProcessType.ProcessIndexes);
                        break;
                    case "ProcessData":
                        db.Cubes[CubeName].Process(ProcessType.ProcessData);
                        break;
                    case "ProcessDefault":
                        db.Cubes[CubeName].Process(ProcessType.ProcessDefault);
                        break;
                    case "ProcessClear":
                        db.Cubes[CubeName].Process(ProcessType.ProcessClear);
                        break;
                    case "ProcessStructure":
                        db.Cubes[CubeName].Process(ProcessType.ProcessStructure);
                        break;
                    case "ProcessScriptCache":
                        db.Cubes[CubeName].Process(ProcessType.ProcessScriptCache);
                        break;
                }
            }


            // Stop the default trace
            svr.SessionTrace.Stop();
            
        }

        static void DefaultTrace_OnEvent(object sender, TraceEventArgs e)
        {
            Debug.WriteLine(String.Format("{0}", e.TextData));
        }

        static void DefaultTrace_Stopped(ITrace sender, TraceStoppedEventArgs e)
        {
            switch (e.StopCause)
            {
                case TraceStopCause.StoppedByUser:
                case TraceStopCause.Finished:
                    Debug.WriteLine(String.Format("Processing completed successfully"));
                    break;

                case TraceStopCause.StoppedByException:
                    Debug.WriteLine(String.Format("Processing failed: {0}",
                        e.Exception.Message));
                    break;
            }
        }

    }
}
